package com.hnust.controller.subject.dialog;

import com.hnust.domain.*;
import com.hnust.fx.FXMLController;
import com.hnust.service.SubjectService;
import com.hnust.service.SubjectServices;
import com.hnust.store.DataStore;
import com.hnust.store.SubjectStore;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.util.StringConverter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * 创建人：zhangRuWang
 * 创建时间：2020/12/8
 * 描述：
 */
@FXMLController
public class ModifySubjectController implements Initializable {

    public Label courseDataLabel;
    public Label chapterLabel;
    public Label targetLabel;
    public Label queTypeLabel;
    @FXML
    private ComboBox<QuestionTypeRecord> questionTypeComb;
    @FXML
    private RadioButton r1;
    @FXML
    private RadioButton r2;
    @FXML
    private RadioButton r3;
    @FXML
    private ComboBox<CourseData> courseComb;
    @FXML
    private ComboBox<CourseChapter> chapterComb;
    @FXML
    private ComboBox<CourseTargetRecord> targetComb;
    @FXML
    private AnchorPane modifySubjectWindow;
    @FXML
    private TextField knowledgePoint;
    @FXML
    private WebView subjectContent;
    @FXML
    private VBox trueVbox;

    @FXML
    private VBox falseVbox;

    @FXML
    private WebView subjectResolve;
    @Autowired
    private SubjectStore subjectStore;
    private SubjectTest subjectDataInfo;
    @Autowired
    private SubjectServices subjectServices;
    private String selectCourseId;
    private String selectChapterId;
    private String selectTargetId;
    private Integer selectQueTypeId;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        courseComb.setConverter(new StringConverter<CourseData>() {
            @Override
            public String toString(CourseData object) {
                return object.getName();
            }
            @Override
            public CourseData fromString(String string) {
                return null;
            }
        });
        courseComb.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                chapterComb.setItems(null);
                targetComb.setItems(null);
                subjectStore.setModifyHasChapter(false);
                subjectStore.setModifyHasTarget(false);
                initSubjectChapter(newValue.getId());
                initSubjectCourseTarget(newValue.getId());
                selectCourseId = newValue.getId();
            }
        });
        chapterComb.setConverter(new StringConverter<CourseChapter>() {
            @Override
            public String toString(CourseChapter object) {
                return object.getContent();
            }
            @Override
            public CourseChapter fromString(String string) {
                return null;
            }
        });
        chapterComb.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                selectChapterId=newValue.getId();
                subjectStore.setModifyHasChapter(true);
            }
        });
        targetComb.setConverter(new StringConverter<CourseTargetRecord>() {
            @Override
            public String toString(CourseTargetRecord object) {
                return object.getContent();
            }
            @Override
            public CourseTargetRecord fromString(String string) {
                return null;
            }
        });
        targetComb.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                selectTargetId=newValue.getId();
                subjectStore.setModifyHasTarget(true);
            }
        });

        questionTypeComb.setConverter(new StringConverter<QuestionTypeRecord>() {
            @Override
            public String toString(QuestionTypeRecord object) {
                return object.getName();
            }
            @Override
            public QuestionTypeRecord fromString(String string) {
                return null;
            }
        });
        questionTypeComb.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                selectQueTypeId=newValue.getId();
            }
        });

        modifySubjectWindow.parentProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                subjectDataInfo=subjectStore.getModifySubject();
                initData();
            }else{
                clearData();
            }
        });
    }

    private void initSubjectCourseTarget(String id) {
        subjectServices.getCourseTargetRecord(targetLabel,id,targetComb,subjectDataInfo.getCourseTargetId());
    }

    private void initSubjectChapter(String id) {
        subjectServices.getCourseChapterList(chapterLabel,chapterComb,id,subjectDataInfo.getChapter_id());
    }

    private void initSubjectType() {
        subjectServices.getQuesTypeList(queTypeLabel,questionTypeComb,subjectDataInfo.getType_id());
    }

    private void initSubjectCourse() {
        subjectServices.getCourseData(courseDataLabel,courseComb,
                0,subjectDataInfo.getCourse_id());
    }

    private void initData() {
        //初始化获取题目课程和类型
        initSubjectCourse();
        initSubjectType();

        if(subjectDataInfo.getDifficult()==3){
            r1.setSelected(true);
        }else if(subjectDataInfo.getDifficult()==2){
            r2.setSelected(true);
        }else{
            r3.setSelected(true);
        }
        knowledgePoint.setText(subjectDataInfo.getKnowledgePointer());
        subjectContent.getEngine().loadContent(subjectDataInfo.getContent());
        subjectResolve.getEngine().loadContent(subjectDataInfo.getResolve());
        if(subjectDataInfo.getAnswer()!=null){
            for(AnswerTestObj a:subjectDataInfo.getAnswer()){
                WebView t=new WebView();
                t.getEngine().loadContent(a.getContent());
                t.setMaxHeight(200);
                if(a.getRight()){
                    trueVbox.getChildren().addAll(t);
                }else{
                    falseVbox.getChildren().addAll(t);
                }
            }
        }
    }

    public void setModifySubject(){
        int selectDifficult=1;
        if(r1.isSelected()){
            selectDifficult=3;
        }else if(r2.isSelected()){
            selectDifficult=2;
        }
        List<AnswerObjRecoed> answerObjRecoeds=new ArrayList<>();
        for(AnswerTestObj i:subjectDataInfo.getAnswer()){
            answerObjRecoeds.add(new AnswerObjRecoed(null,i.getContent(),i.getRight()));
        }
        //修改
        subjectStore.setModifySubjectDataInfo(new SubjectDataInfo(
                subjectDataInfo.getId(),subjectDataInfo.getContent(),null,selectCourseId,
                null,selectChapterId,null,selectQueTypeId.toString(),selectDifficult,
                subjectDataInfo.getResolve(),
                knowledgePoint.getText(),null,selectTargetId,0,
                answerObjRecoeds,0
        ));
    }

    public void clearData(){
        r1.setSelected(false);r2.setSelected(false);r3.setSelected(false);
        questionTypeComb.setValue(null);courseComb.setValue(null);chapterComb.setValue(null);
        knowledgePoint.setText(null);targetComb.setValue(null);
        subjectContent.getEngine().loadContent(null);
        subjectResolve.getEngine().loadContent(null);
        trueVbox.getChildren().clear();
        falseVbox.getChildren().clear();
    }

    public String parseHtml(String s){
        Document d= Jsoup.parse(s);
        String content=d.getElementsByTag("body").toString();
        int b=content.indexOf('>');
        int e=content.lastIndexOf('<');
        return content.substring(b+1,e).trim();
    }
}
